<template>
  <el-container class="common-layout">
    <el-container>
      <el-main class="main-content">
        <!-- 我的学习进度 -->
        <!-- <el-row :gutter="20" class="mb-4">
          <el-col :span="24">
            <el-card shadow="hover">
              <template #header>
                <div class="card-header-title">我的学习进度</div>
              </template>
              <el-row :gutter="20">
                <el-col :xs="24" :sm="12" :md="8">
                  <div class="d-flex align-items-center">
                    <div class="flex-shrink-0">
                      <div class="icon-circle bg-primary text-white">
                        <i class="bi bi-book"></i>
                      </div>
                    </div>
                    <div class="flex-grow-1 ms-3">
                      <div class="text-muted">已学课程</div>
                      <div class="metric-value">
                        12 <small class="text-muted">/ 20</small>
                      </div>
                    </div>
                  </div>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8">
                  <div class="d-flex align-items-center">
                    <div class="flex-shrink-0">
                      <div class="icon-circle bg-success text-white">
                        <i class="bi bi-clock-history"></i>
                      </div>
                    </div>
                    <div class="flex-grow-1 ms-3">
                      <div class="text-muted">学习时长</div>
                      <div class="metric-value">
                        32 <small class="text-muted">小时</small>
                      </div>
                    </div>
                  </div>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8">
                  <div class="d-flex align-items-center">
                    <div class="flex-shrink-0">
                      <div class="icon-circle bg-info text-white">
                        <i class="bi bi-award"></i>
                      </div>
                    </div>
                    <div class="flex-grow-1 ms-3">
                      <div class="text-muted">获得证书</div>
                      <div class="metric-value">
                        3 <small class="text-muted">个</small>
                      </div>
                    </div>
                  </div>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row> -->

        <!-- 搜索和筛选表单 -->
        <!-- <el-card class="box-card mb-4" shadow="never">
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="课程名称">
              <el-input
                placeholder="请输入课程名称"
                v-model="searchForm.name"
              ></el-input>
            </el-form-item>
            <el-form-item label="课程分类">
              <el-select placeholder="请选择分类" v-model="searchForm.category">
                <el-option
                  v-for="item in categories"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="难度级别">
              <el-select
                placeholder="请选择难度"
                v-model="searchForm.difficulty"
              >
                <el-option
                  v-for="item in levels"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSearch">查询</el-button>
              <el-button @click="onReset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card> -->

        <!-- 进行中的课程 -->
        <el-row :gutter="20" class="mb-4">
          <el-col :span="24">
            <el-card shadow="hover">
              <template #header>
                <div
                  class="card-header-title d-flex justify-content-between align-items-center"
                >
                  <span>进行中的课程</span>
                  <el-button type="primary" link @click="goToCourseList"
                    >查看全部课程
                  </el-button>
                </div>
              </template>
              <el-row :gutter="20">
                <el-col
                  v-for="course in filteredCourses"
                  :key="course.id"
                  :xs="24"
                  :sm="12"
                  :md="8"
                  :lg="8"
                >
                  <el-card shadow="hover" class="course-card">
                    <el-image
                      :src="course.coverUrl"
                      class="course-img"
                      alt="课程封面"
                    >
                      <template #error>
                        <div class="image-slot">
                          <el-icon>
                            <Camera />
                          </el-icon>
                          <p>暂无封面</p>
                        </div>
                      </template>
                    </el-image>
                    <div class="course-body">
                      <div
                        class="d-flex justify-content-between align-items-center mb-2"
                      >
                        <el-tag :type="getCategoryType(course.categoryName)"
                          >{{ course.categoryName }}
                        </el-tag>
                        <!--                        <small class="text-muted"-->
                        <!--                          >进度: {{ course.progress }}%</small-->
                        <!--                        >-->
                      </div>
                      <h5 class="course-title">{{ course.title }}</h5>
                      <!--                      <el-progress-->
                      <!--                        :percentage="course.progress"-->
                      <!--                        :stroke-width="5"-->
                      <!--                        :show-text="false"-->
                      <!--                        :color="course.progressColor"-->
                      <!--                      ></el-progress>-->
                      <p class="course-text text-muted small">
                        <!--                        最近学习: {{ course.lastStudyDate }}-->
                      </p>
                      <el-button
                        type="primary"
                        class="w-100"
                        @click="goToCourseDetail(course.id)"
                        >继续学习
                      </el-button>
                    </div>
                  </el-card>
                </el-col>
                <el-col v-if="filteredCourses.length === 0" :span="24">
                  <el-empty description="没有找到相关课程"></el-empty>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>

        <!-- 推荐课程 -->
        <el-row :gutter="20" class="mb-4">
          <el-col :span="24">
            <el-card shadow="hover">
              <template #header>
                <div class="card-header-title">推荐课程</div>
              </template>

              <el-row :gutter="20">
                <el-col
                  :span="8"
                  v-for="course in courseStore.recommended"
                  :key="course.course.id"
                >
                  <el-card shadow="hover" class="course-card">
                    <el-image
                      :src="course.course.coverUrl"
                      class="course-img"
                      alt="课程封面"
                    >
                      <template #error>
                        <div class="image-slot">
                          <el-icon>
                            <Camera />
                          </el-icon>
                          <p>暂无封面</p>
                        </div>
                      </template>
                    </el-image>
                    <div class="course-card-body">
                      <div
                        class="d-flex justify-content-between align-items-center mb-2"
                      >
                        <el-tag
                          :type="getCategoryType(course.course.categoryName)"
                          >{{ course.course.categoryName }}
                        </el-tag>
                        <el-rate
                          :model-value="course.course.star"
                          disabled
                          show-score
                          text-color="#ff9900"
                          score-template="{value}分"
                        ></el-rate>
                      </div>
                      <h5 class="card-title">{{ course.course.title }}</h5>
                      <p class="card-text text-muted">
                        {{ course.course.description }}
                      </p>
                      <div
                        class="d-flex justify-content-between align-items-center"
                      >
                        <small class="text-muted">
                          <i class="bi bi-people"></i>
                          {{ course.selectNumber }}人学习
                        </small>
                        <small class="text-muted">
                          <i class="bi bi-clock"></i>
                          {{ course.course.lastingTime }}小时课程
                        </small>
                      </div>
                    </div>
                    <div class="course-card-footer">
                      <el-button
                        type="primary"
                        @click="goToCourseDetail(course.course.id)"
                        >查看详情
                      </el-button>
                    </div>
                  </el-card>
                </el-col>
                <el-col v-if="filteredCourses.length === 0" :span="24">
                  <el-empty description="没有找到相关课程"></el-empty>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, reactive } from "vue";
import { Camera, Search } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/user.js";
import { useCourseStore } from "@/stores/course.js";
import router from "@/router/index.js";

const userStore = useUserStore();
userStore.getMyCourse();
const courseStore = useCourseStore();
courseStore.getRecommendedCourses();
const searchForm = reactive({
  name: "",
  category: "all",
  difficulty: "all",
});

const categories = [
  { label: "全部课程", value: "all" },
  { label: "基础护理", value: "基础护理" },
  { label: "专科护理", value: "专科护理" },
  { label: "急救技能", value: "急救技能" },
  { label: "职业发展", value: "职业发展" },
  { label: "公共卫生", value: "公共卫生" },
  { label: "老年护理", value: "老年护理" },
];

const levels = [
  { label: "全部级别", value: "all" },
  { label: "入门级", value: "0" },
  { label: "基础级", value: "1" },
  { label: "进阶级", value: "2" },
  { label: "专家级", value: "3" },
];

const onSearch = () => {
  console.log("搜索条件:", searchForm);
};

const onReset = () => {
  searchForm.name = "";
  searchForm.category = "all";
  searchForm.difficulty = "all";
  console.log("重置搜索条件:", searchForm);
};

const filteredCourses = computed(() => {
  return userStore.myClass.filter((course) => {
    const nameMatch =
      !searchForm.name || course.course.title.includes(searchForm.name);
    const categoryMatch =
      searchForm.category === "all" ||
      course.course.categoryName === searchForm.category;
    const difficultyMatch =
      searchForm.difficulty === "all" ||
      course.course.difficulty.toString() === searchForm.difficulty;
    return nameMatch && categoryMatch && difficultyMatch;
  });
});

const getCategoryType = (category) => {
  switch (category) {
    case "基础护理":
      return "primary";
    case "专科护理":
      return "success";
    case "急救技能":
      return "warning";
    case "职业发展":
      return "info";
    case "公共卫生":
      return "danger";
    case "老年护理":
      return "";
    default:
      return "";
  }
};

const goToCourseList = () => {
  router.push("/student/course-list");
};

const goToCourseDetail = (course_id) => {
  router.push(`/student/course-detail/` + course_id);
};
</script>

<style scoped>
.common-layout {
  height: 100vh;
}

.el-aside {
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.05);
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu-item i {
  margin-right: 5px;
}

.header-nav {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
}

.card-header-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.icon-circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
}

.bg-primary {
  background-color: #409eff !important;
}

.bg-success {
  background-color: #67c23a !important;
}

.bg-info {
  background-color: #909399 !important;
}

.metric-value {
  font-size: 24px;
  font-weight: bold;
}

.course-card {
  height: 100%;
}

.course-img {
  width: 100%;
  height: 480px;
  object-fit: cover;
}

.course-body {
  padding: 15px;
}

.course-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.course-text {
  font-size: 14px;
  line-height: 1.5;
}

.notification-badge {
  position: absolute;
  top: -5px;
  right: -5px;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 14px;
  cursor: pointer;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .hidden-md-and-down {
    display: none !important;
  }
}
</style>
